package com.txunda.zhuohired.fgt.user.home;

import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.util.L;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.adapter.user.home.CompanyJobListAdapter;
import com.txunda.zhuohired.base.BaseFgt;
import com.txunda.zhuohired.bean.user.home.CompanyOneBean;
import com.txunda.zhuohired.http.pst.MemberHomePst;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/5 11:18
 * 功能描述：
 * 联系方式：jiahuanchen@126.com
 */

public class CompanyOneFgt extends BaseFgt {

    private String u_id;
    private int status_type = 2;

    @ViewInject(R.id.lv_company_one)
    private ListView lv_company_one;

    @ViewInject(R.id.ll_no_data)
    private LinearLayout ll_no_data;

    private CompanyJobListAdapter companyJobListAdapter;
    private List<CompanyOneBean.DataBean.InfoDataBean> companyInfoList;

    private MemberHomePst memberHomePst;
    private String token = "";

    @Override
    protected void immersionInit() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fgt_company_one;
    }

    @Override
    protected void initialized() {
        memberHomePst = new MemberHomePst(this);
        token = application.getUserInfo().get("data");
        companyInfoList = new ArrayList<>();
    }

    /**
     * 每次fragment可见时
     * <p/>
     * 请求接口
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    initHttp();
                }
            }, 100);
        } else {
            // 不可见时不执行操作
        }
    }

    @Override
    protected void requestData() {
        //        initHttp();
        //        initAdapter();
    }

    private void initHttp() {
        memberHomePst.companyInfo(u_id, String.valueOf(status_type), token);
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("companyInfo")) {
            L.e("==========companyInfo==========", jsonStr);
            CompanyOneBean companyOneBean = GsonUtil.GsonToBean(jsonStr, CompanyOneBean.class);
            companyInfoList = companyOneBean.getData().getInfo_data();
            if (companyInfoList.size() == 0) {
                noData(2);
            } else {
                noData(1);
                initAdapter();
            }
            return;
        }
    }

    private void noData(int type) {
        if (type == 1) {
            lv_company_one.setVisibility(View.VISIBLE);
            ll_no_data.setVisibility(View.GONE);
        } else {
            ll_no_data.setVisibility(View.VISIBLE);
            lv_company_one.setVisibility(View.GONE);
        }
    }

    private void initAdapter() {
        companyJobListAdapter = new CompanyJobListAdapter(getActivity(), companyInfoList);
        lv_company_one.setAdapter(companyJobListAdapter);
    }

    public static CompanyOneFgt getFgt(int type, String u_id) {
        CompanyOneFgt procurerFgt = new CompanyOneFgt();
        procurerFgt.u_id = u_id;
        procurerFgt.status_type = type;
        return procurerFgt;
    }
}
